using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.SaleReturn
{
    public partial class ARDocumentPaymentDetailsGridControl : THSGridControl
    {
        public override void InitializeControl()
        {
            base.InitializeControl();
            this.UseEmbeddedNavigator = false;
            this.Enter += new EventHandler(GridControl_Enter);
        }

        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            gridView.Columns["ARPaymentMethodCombo"].AppearanceCell.BackColor = Color.WhiteSmoke;
            gridView.Columns["ARDocumentPaymentDetailAmount"].OptionsColumn.AllowEdit = true;
            gridView.Columns["ARDocumentPaymentDetailInfo"].OptionsColumn.AllowEdit = true;
            gridView.OptionsSelection.EnableAppearanceFocusedRow = false;
            return gridView;
        }

        public override void InitGridControlDataSource()
        {
            SaleReturnEntities entity = (SaleReturnEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            entity.ARDocumentPaymentDetailsList.Clear();

            //Filter payment methods which checked_Hieu.nguyen_15/4/2010
            ADConfigValuesController objADConfigValuesController = new ADConfigValuesController();
            DataSet ds = objADConfigValuesController.GetADConfigValuesByGroup(ADConfigValueUtility.ADConfigValuePaymentMethod);
            foreach (DataRow configRow in ds.Tables[0].Rows)
            {
                if (configRow["ADConfigKeyDesc"].ToString() == "1")
                {
                    ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo = new ARDocumentPaymentDetailsInfo();
                    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo = configRow["ADConfigKeyValue"].ToString();
                    objDocumentPaymentDetailsInfo.ARPaymentMethodDisplayText = configRow["ADConfigText"].ToString();
                        entity.ARDocumentPaymentDetailsList.Add(objDocumentPaymentDetailsInfo);
                }
            }

            //foreach (DataRow configRow in ADConfigValueUtility.glbdsConfigValues.Tables[ADConfigValueUtility.ADConfigValuePaymentMethod].Rows)
            //{
            //    ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo = new ARDocumentPaymentDetailsInfo();
            //    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo = configRow["Value"].ToString();
            //    objDocumentPaymentDetailsInfo.ARPaymentMethodDisplayText = configRow["Text"].ToString();
            //    if (objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodCash ||
            //        objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodCreditNote ||
            //        objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodGiftVoucher)
            //        entity.ARDocumentPaymentDetailsList.Add(objDocumentPaymentDetailsInfo);
            //}
            this.DataSource = entity.ARDocumentPaymentDetailsList;
            this.RefreshDataSource();
        }

        #region GridControl Event Handlers
        private void GridControl_Enter(object sender, EventArgs e)
        {
            ProposeRemainingAmount();
        }

        protected override void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            base.GridView_FocusedRowChanged(sender, e);

            ProposeRemainingAmount();
        }
        #endregion

        private void ProposeRemainingAmount()
        {
            SaleReturnEntities entity = (SaleReturnEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            ARSaleReturnsInfo objSaleReturnsInfo = (ARSaleReturnsInfo)entity.MainObject;
            if (entity.ARDocumentPaymentDetailsList.CurrentIndex >= 0)
            {
                ARDocumentPaymentDetailsInfo currentPayment = entity.ARDocumentPaymentDetailsList[entity.ARDocumentPaymentDetailsList.CurrentIndex];
                double amount = 0;
                foreach (ARDocumentPaymentDetailsInfo objDocumentPaymentDetaisInfo in entity.ARDocumentPaymentDetailsList)
                    if (objDocumentPaymentDetaisInfo.ARPaymentMethodCombo != currentPayment.ARPaymentMethodCombo)
                        amount += objDocumentPaymentDetaisInfo.ARDocumentPaymentDetailAmount;

                if (objSaleReturnsInfo.ARSaleReturnTotalAmount > amount)
                    currentPayment.ARDocumentPaymentDetailAmount = objSaleReturnsInfo.ARSaleReturnTotalAmount - amount;
                this.RefreshDataSource();
            }
        }

    }
}
